package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.common.factory.PaperFactory;
import com.mianyang.zhangsan.common.util.StringUtils;
import com.mianyang.zhangsan.mapper.SecPaperMapper;
import com.mianyang.zhangsan.model.entity.SecBank;
import com.mianyang.zhangsan.model.entity.SecPaper;
import com.mianyang.zhangsan.model.entity.SecPaperBank;
import com.mianyang.zhangsan.model.entity.SecTemplate;
import com.mianyang.zhangsan.service.SecPaperBankService;
import com.mianyang.zhangsan.service.SecPaperService;
import com.mianyang.zhangsan.service.SecTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author jiangjr
*/
@Service
public class SecPaperServiceImpl extends ServiceImpl<SecPaperMapper, SecPaper>
    implements SecPaperService{
    @Autowired
    private SecTemplateService templateService;
    @Autowired
    private SecPaperBankService paperBankService;

    @Override
    public SecPaper generatorPaper(Long paperTemplateId, String paperName, String paperDesc, Long bankId) {
        SecTemplate template = templateService.getById(paperTemplateId);
        if (template != null) {
            SecPaper paper = PaperFactory.createByTemplate(template);
            paper.setPaperName(paperName);
            paper.setPaperDesc(paperDesc);
            // 保存试卷基本信息
            save(paper);
            // 生成试卷与题库的关联
            SecPaperBank paperBank = new SecPaperBank();
            paperBank.setPaperId(paper.getPaperId());
            paperBank.setBankId(bankId);
            paperBank.setProblems(template.getProblems());
            paperBank.setProblemType(template.getPaperType());
            paperBankService.save(paperBank);
            // 生成试卷实例
            return paper;
        }
        return null;
    }

    @Override
    public Page<SecPaper> selectPaperByPage(SecPaper paper, int current, int pageSize) {
        QueryWrapper<SecPaper> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(paper.getPaperName())) {
            wrapper.like("paper_name", paper.getPaperName());
        }
        if (paper.getPaperType() != null) {
            wrapper.eq("paper_type", paper.getPaperType());
        }
        return page(new Page<>(current, pageSize), wrapper);
    }

    @Override
    public void deleteAndPaperInst(Long paperId) {
        // 删除试卷信息
        removeById(paperId);
        // 删除试卷的实例信息
    }
}




